package com.stylefeng.guns.biz.business.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.biz.business.model.TBusiness;
import com.stylefeng.guns.biz.business.service.ITBusinessService;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.app.service.IBusinessClassifyService;
import com.stylefeng.guns.modular.system.model.BusinessClassify;

/**
 * 内容管理控制器
 *
 * @author fengshuonan
 * @Date 2018-06-14 06:33:39
 */
@Controller
@RequestMapping("/tBusiness")
public class TBusinessController extends BaseController {

    private String PREFIX = "/app/tBusiness/";

    @Autowired
    private ITBusinessService tBusinessService;
    

    @Autowired
    private IBusinessClassifyService businessClassifyService;

    /**
     * 跳转到内容管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "tBusiness.html";
    }

    /**
     * 跳转到添加内容管理
     */
    @RequestMapping("/tBusiness_add")
    public String tBusinessAdd(Model model,String appId) {
    	
    	 EntityWrapper<BusinessClassify> wrapper=new EntityWrapper<>();
         wrapper.eq("app_id", appId);
         
     	List<BusinessClassify> list  = businessClassifyService.selectList(wrapper);
    	model.addAttribute("list", list);
        return PREFIX + "tBusiness_add.html";
    }

    /**
     * 跳转到修改内容管理
     */
    @RequestMapping("/tBusiness_update/{tBusinessId}")
    public String tBusinessUpdate(@PathVariable Long tBusinessId, Model model) {
        TBusiness tBusiness = tBusinessService.selectById(tBusinessId);
        model.addAttribute("item",tBusiness);
        LogObjectHolder.me().set(tBusiness);
        
        EntityWrapper<BusinessClassify> wrapper=new EntityWrapper<>();
        wrapper.eq("app_id", tBusiness.getAppIdKey());
        
    	List<BusinessClassify> list  = businessClassifyService.selectList(wrapper);
    	model.addAttribute("list", list);
    	
        return PREFIX + "tBusiness_edit.html";
    }

    /**
     * 获取内容管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String appid) {
    	EntityWrapper<TBusiness> wrapper=new EntityWrapper<>();
    	wrapper.eq("app_id_key", appid);
        return tBusinessService.selectList(wrapper);
    }

    /**
     * 新增内容管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(TBusiness tBusiness) {
        tBusinessService.insert(tBusiness);
        return SUCCESS_TIP;
    }

    /**
     * 删除内容管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long tBusinessId) {
        tBusinessService.deleteById(tBusinessId);
        return SUCCESS_TIP;
    }

    /**
     * 修改内容管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(TBusiness tBusiness) {
        tBusinessService.updateById(tBusiness);
        return SUCCESS_TIP;
    }

    /**
     * 内容管理详情
     */
    @RequestMapping(value = "/detail/{tBusinessId}")
    @ResponseBody
    public Object detail(@PathVariable("tBusinessId") Long tBusinessId) {
        return tBusinessService.selectById(tBusinessId);
    }
}
